﻿using Prism.Unity;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF_CoverView_Demo.Models;

namespace WPF_CoverView_Demo.ViewModels
{

    public class MainWindowViewModel : ReactiveObject
    {
        [Reactive]
        public ObservableCollection<ShowedData> ShowedDatas { get; set; }

        private Random random = new Random();  // 用于生成随机数据

        public MainWindowViewModel()
        {
            // 初始化 10 个通道
            ShowedDatas = new ObservableCollection<ShowedData>()
            {
                new ShowedData(1, "Channel 1", "Sensor 1", "Description 1", "Type A", "Unit A", 0.5),
                new ShowedData(2, "Channel 2", "Sensor 2", "Description 2", "Type B", "Unit B", 1.0),
                new ShowedData(3, "Channel 3", "Sensor 3", "Description 3", "Type C", "Unit C", 1.5),
                new ShowedData(4, "Channel 4", "Sensor 4", "Description 4", "Type D", "Unit D", 2.0),
                new ShowedData(5, "Channel 5", "Sensor 5", "Description 5", "Type E", "Unit E", 2.5),
                new ShowedData(6, "Channel 6", "Sensor 6", "Description 6", "Type F", "Unit F", 3.0),
                new ShowedData(7, "Channel 7", "Sensor 7", "Description 7", "Type G", "Unit G", 3.5),
                new ShowedData(8, "Channel 8", "Sensor 8", "Description 8", "Type H", "Unit H", 4.0),
                new ShowedData(9, "Channel 9", "Sensor 9", "Description 9", "Type I", "Unit I", 4.5),
                new ShowedData(10, "Channel 10", "Sensor 10", "Description 10", "Type J", "Unit J", 5.0)
            };

            Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(100);
                    AddRandomData();
                }
            });
        }

        // 动态添加随机点数据到某个通道
        private void AddRandomData()
        {

            var selectedChannel = ShowedDatas[random.Next(ShowedDatas.Count)];

            var newDataPoint = new DataPoint
            {
                Number = selectedChannel.DataPoints.Count + 1,
                Amplitude = random.NextDouble() * 100,
                Phase = random.NextDouble() * 360,
                Speed = random.NextDouble() * 50
            };

            App.Current.Dispatcher.Invoke(() =>
            {
                selectedChannel.DataPoints.Add(newDataPoint);
            });
        }
    }
}
